import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/dashboard/analysis',
    hidden: true,
    children: [
      {
        path: '/dashboard/analysis',
        component: () => import('@/views/dashboard/Analysis'),
        name: 'Analysis',
        meta: { title: '首页', icon: 'home', affix: true }
      }
    ]
  },
  {
    path: '/erp/sale',
    component: Layout,
    name: 'erp-sale',
    meta: { title: '销售管理', icon: 'shop' },
    hidden: true,
    children: [
      {
        path: '/erp/sale/quot',
        component: () => import('@/views/erp/sale/SalQuotList.vue'),
        name: 'erp-sale-quot',
        meta: { title: '销售报价x', keepAlive: false },
      },
      {
        path: '/erp/sale/order',
        component: () => import('@/views/erp/sale/SalOrderList.vue'),
        name: 'erp-sale-order',
        meta: { title: '销售订单x', keepAlive: true },
      },
      {
        path: '/erp/sale/statistics',
        component: () => import('@/views/layouts/RouteView.vue'),
        name: 'erp-sale-statistics',
        meta: { title: '销售统计x', icon: 'RouteView' }, // icon根据实际配置调整
        children: [
          {
            path: 'order/progress',
            component: () => import('@/views/erp/sale/statistics/SalOrderProgress.vue'),
            name: 'erp-sale-statistics-order-progress',
            meta: { title: '销售订单执行情况', keepAlive: false },
          },
          {
            path: 'order/profit',
            component: () => import ('@/views/erp/sale/statistics/SalOrderProfit.vue'),
            name: 'erp-sale-statistics-order-profit',
            meta: { title: ' 销售订单毛利润 ', keepAlive: false },
            },
            {
            path: 'order/profit/detail',
            component: () => import ('@/views/erp/sale/statistics/SalOrderProfitDetail.vue'),
            name: 'erp-sale-statistics-order-profit-detail',
            meta: { title: ' 销售订单毛利润明细 ', keepAlive: false },
            },
            {
            path: 'order/sum',
            component: () => import ('@/views/erp/sale/statistics/SalOrderMonthSum.vue'),
            name: 'erp-sale-statistics-order-sum',
            meta: { title: ' 销售订单汇总 ', keepAlive: false },
            },
            {
            path: 'order/sum/dept',
            component: () => import ('@/views/erp/sale/statistics/SalOrderMonthSumDept.vue'),
            name: 'erp-sale-statistics-order-sum-dept',
            meta: { title: ' 销售订单汇总 - 部门 ', keepAlive: false },
            },
            {
            path: 'order/sum/operator',
            component: () => import ('@/views/erp/sale/statistics/SalOrderMonthSumOperator.vue'),
            name: 'erp-sale-statistics-order-sum-operator',
            meta: { title: ' 销售订单汇总 - 业务员 ', keepAlive: false },
            },
            {
            path: 'order/sum/customer',
            component: () => import ('@/views/erp/sale/statistics/SalOrderMonthSumCustomer.vue'),
            name: 'erp-sale-statistics-order-sum-customer',
            meta: { title: ' 销售订单汇总 - 客户 ', keepAlive: false },
            },
            {
            path: 'order/sum/material',
            component: () => import ('@/views/erp/sale/statistics/SalOrderMonthSumMaterial.vue'),
            name: 'erp-sale-statistics-order-sum-material',
            meta: { title: ' 销售订单汇总 - 物料 ', keepAlive: false },
            }
        ]
      }
    ]
  },
  {
    path: '/erp/purchase',
    component: Layout,
    name: 'erp-purchase',
    meta: { title: '采购管理', icon: 'shopping-cart' },
    hidden: true,
    children: [
      {
        path: '/erp/purchase/payment2011/req',
        component: () => import('@/views/erp/purchase/PurPrepayment2011ReqList.vue'),
        name: 'erp-purchase-payment2011-req',
        meta: { title: '采购预付申请', keepAlive: true },
      },
      {
        path: '/erp/purchase/payment2021/req',
        component: () => import('@/views/erp/purchase/PurPayment2021ReqList.vue'),
        name: 'erp-purchase-payment2021-req',
        meta: { title: '采购付款申请', keepAlive: true },
      },
    ]
  },
  {
    path: '/erp/stock',
    component: Layout,
    name: 'erp-stock',
    meta: { title: '库存管理', icon: 'hdd' },
    hidden: true,
    children: [
      {
        path: 'in',
        component: () => import('@/views/layouts/RouteView.vue'),
        name: 'erp-stock-in',
        meta: { title: '入库管理' },
        children: [
          {
            path: 'purchase',
            component: () => import('@/views/erp/stock/in/PurInList.vue'),
            name: 'erp-stock-in-purchase',
            meta: { title: '采购入库', keepAlive: false },
          },
          {
            path: 'purchase/rubric',
            component: () => import('@/views/erp/stock/in/RubricPurInList.vue'),
            name: 'erp-stock-in-purchase-rubric',
            meta: { title: '采购退货出库(红入)', keepAlive: false },
          },
          {
            path: 'check',
            component: () => import('@/views/erp/stock/in/CheckInList.vue'),
            name: 'erp-stock-in-check',
            meta: { title: '盘盈入库', keepAlive: false },
          },
          {
            path: 'swell',
            component: () => import('@/views/erp/stock/in/SwellInList.vue'),
            name: 'erp-stock-in-swell',
            meta: { title: '涨吨入库', keepAlive: false },
          },
          {
            path: 'other',
            component: () => import('@/views/erp/stock/in/OtherInList.vue'),
            name: 'erp-stock-in-other',
            meta: { title: '其他入库', keepAlive: false },
          }
        ]
      },
      {
        path: 'out',
        component: () => import('@/views/layouts/RouteView.vue'),
        name: 'erp-stock-out',
        meta: { title: '出库管理' },
        children: [
          {
            path: 'sale',
            component: () => import('@/views/erp/stock/out/SalOutList.vue'),
            name: 'erp-stock-out-sale',
            meta: { title: '销售出库', keepAlive: false },
          },
          {
            path: 'sale/rubric',
            component: () => import('@/views/erp/stock/out/RubricSalOutList.vue'),
            name: 'erp-stock-out-sale-rubric',
            meta: { title: '销售退货入库(红出)', keepAlive: false },
          },
          {
            path: 'check',
            component: () => import('@/views/erp/stock/out/CheckOutList.vue'),
            name: 'erp-stock-out-check',
            meta: { title: '盘亏出库', keepAlive: false },
          },
          {
            path: 'other',
            component: () => import('@/views/erp/stock/out/OtherOutList.vue'),
            name: 'erp-stock-out-other',
            meta: { title: '其他出库', keepAlive: false },
          }
        ]
      },
      {
        path: 'other',
        component: () => import('@/views/layouts/RouteView.vue'),
        name: 'erp-stock-other',
        meta: { title: '其他管理' },
        children: [
          {
            path: 'move',
            component: () => import('@/views/erp/stock/other/MoveList.vue'),
            name: 'erp-stock-other-move',
            meta: { title: '库存调拨', keepAlive: false },
          },
          {
            path: 'cost',
            component: () => import('@/views/erp/stock/other/ChangeCostList.vue'),
            name: 'erp-stock-other-cost',
            meta: { title: '成本调整', keepAlive: false },
          },
          {
            path: 'check',
            component: () => import('@/views/erp/stock/other/CheckList.vue'),
            name: 'erp-stock-other-check',
            meta: { title: '库存盘点', keepAlive: false },
          }
        ]
      },
      {
        path: 'inventory',
        component: () => import('@/views/layouts/RouteView.vue'),
        name: 'erp-stock-inventory',
        meta: { title: '即时库存' },
        children: [
          {
            path: 'm',
            component: () => import('@/views/erp/stock/inventory/StkInventoryM.vue'),
            name: 'erp-stock-inventory-m',
            meta: { title: '物料库存', keepAlive: false },
          },
          {
            path: 'mb',
            component: () => import('@/views/erp/stock/inventory/StkInventoryMb.vue'),
            name: 'erp-stock-inventory-mb',
            meta: { title: '物料批次库存', keepAlive: false },
          },
          {
            path: 'wm',
            component: () => import('@/views/erp/stock/inventory/StkInventoryWm.vue'),
            name: 'erp-stock-inventory-wm',
            meta: { title: '仓库物料库存', keepAlive: false },
          },
          {
            path: 'wmb',
            component: () => import('@/views/erp/stock/inventory/StkInventoryList.vue'),
            name: 'erp-stock-inventory-wmb',
            meta: { title: '详细库存', keepAlive: false },
          }
        ]
      },
      {
        path: 'statistics',
        component: () => import('@/views/layouts/RouteView.vue'),
        name: 'erp-stock-statistics',
        meta: { title: '出入库统计' },
        children: [
          {
            path: 'io/sum/month',
            component: () => import('@/views/erp/stock/statistics/StkIoMonthSumWm.vue'),
            name: 'erp-stock-statistics-io-sum-month',
            meta: { title: '出入库月汇总', keepAlive: false },
          },
          {
            path: 'io/sum/day',
            component: () => import('@/views/erp/stock/statistics/StkIoDaySumWmb.vue'),
            name: 'erp-stock-statistics-io-sum-day',
            meta: { title: '出入库日汇总', keepAlive: false },
          },
          {
            path: 'io/detail',
            component: () => import('@/views/erp/stock/statistics/StkIoDetail.vue'),
            name: 'erp-stock-statistics-io-detail',
            meta: { title: '出入库明细', keepAlive: false },
          }
        ]
      }
    ]
  },
  
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' }
      }
    ]
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
      }
    ]
  }
]

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push;
let routerReplace = Router.prototype.replace;
// push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}

export default new Router({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
